<?php
/** 
 *  Create tab navi
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses       Zend_View_Helper_Abstract
 * @package    Rockitbos
 * @subpackage Helper
 * @todo       Changed to multilingual, integrate acl check
 */ 
class Rockitbos_View_Helper_Tab extends Zend_View_Helper_Abstract
{

    /**
     * Request form front controller
     * 
     * @var App_Model_Request
     */
    protected $_request = null;
    
    /**
     * Html Dropdown template
     * 
     * @var string 
     */
    protected $_htmlDropdown = '
        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                %s<b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
                %s
            </ul>
        </li>';
    
    /**
     * Html list link template
     * 
     * @var string 
     */
    protected $_htmlLink = '
        <li class=""%s">
            <a href="%s">
                %s
            </a>
        </li>';
    
    /**
     * Html dropdown subtitle template
     * 
     * @var string 
     */
    protected $_htmlDropdownSubtitle = '
        <li class="%s">%s</li>';

    /**
     * Tab app navigation
     * 
     * @return string App navigation
     */
    public function tab() {

        $nav = null;
        $this->_request = new App_Model_Request();

        foreach ($this->getTabConfig() as $controller => $label) {

            if(is_array($label)) {
                $nav .= $this->_getDropdown($controller, $label);
            } else {
                $nav .= $this->_getLink($controller, $label);
            }
                
        }
        return $nav;
    }
    
    /**
     * Config data for tab navigation
     * 
     * @param string $language
     * 
     * @return array Config data 
     */
    public function getTabConfig($language = "de")
    {
        $config = new Zend_Config_Ini(
            APPLICATION_PATH . "/modules/" . $this->_request->getCurrentApp() . "/configs/app.ini",
            "tab"
        );
        return $config->{$language}->toArray();
    }

    /**
     * Get dropdown navigation
     * 
     * @param string $controller
     * @param array  $label
     * 
     * @return null|string
     */
    protected function _getDropdown($controller, $label)
    {
        $dropdownNav = null;
        $dropdownLabel = array_shift($label);

        foreach ($label as $do => $sublabel) {

            if(strpos($do, "divider") !== false) {

                if ($sublabel === "---") {
                    $dropdownNav .= sprintf(
                        $this->_htmlDropdownSubtitle,
                        "divider",
                        null
                    );
                } else {
                    $dropdownNav .= sprintf(
                    $this->_htmlDropdownSubtitle,
                    "nav-header",
                    $sublabel
                    );
                }
            } else if ($this->_isAllowed($this->_request->getCurrentApp(), $controller, $do)) {
                
                $dropdownNav .=
                    sprintf(
                        $this->_htmlLink,
                        null,
                        $this->view->url(
                            array(
                                "module" => "rockitbos",
                                "app" => $this->_request->getCurrentApp(),
                                "tab" => $controller,
                                "do" => $do),
                                null,
                                true
                            ),
                            $sublabel
                        );
            }
        }
        
        if ($dropdownNav === null) {
            return null;
        }
        return sprintf($this->_htmlDropdown, $dropdownLabel, $dropdownNav);
    }

    /**
     * Get navigation link
     * 
     * @param string $controller
     * @param string $label
     * 
     * @return null|string
     */
    protected function _getLink($controller, $label)
    {
        $active = null;
        if($this->_request->getCurrentTab() == $controller) {
            $active = "active";
        }

        if ($this->_isAllowed($this->_request->getCurrentApp(), $controller)) {
        
            return sprintf(
                $this->_htmlLink,
                $active,
                $this->view->url(
                    array(
                        "module" => "rockitbos",
                        "app" => $this->_request->getCurrentApp(),
                        "tab" => $controller
                    ),
                    NULL,
                    TRUE
                ),
                $label
            );
        }
        return null;
        
    }
    
    /**
     * Check if access of the action is allowed
     * 
     * @param string $module
     * @param string $controller
     * @param string $action
     * 
     * @return boolean 
     */
    protected function _isAllowed($module, $controller, $action = "index")
    {
        try {
            $resource = $module. "_" . $controller;

            $auth = Zend_Auth::getInstance();
            $user = $auth->getStorage()->read();

            if ($user["Zend_Acl"]->isAllowed(null, $resource, $action)) {
                return true;
            }
            return false;

        } catch (Zend_Exception $e) {

            error_log(print_r($e, true));
            return false;
        }
    }
}